﻿Imports DevComponents.DotNetBar
Imports quanlydulich.Helpers

Partial Public Class DIADIEM
    Inherits Office2007Form
    Private objDS As DataSet
    ' Event handler is used to transfer data to receiver
    Public Event Handler(ByVal sender As Object, ByVal e As MyEvent)

    Private Sub DIADIEM_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
    End Sub

    ' Load list of DIADIEM from DB
    Public Sub LoadData()
        Dim objBUS As DIADIEMBUS = New DIADIEMBUS(GlobalVars.g_strConnectionString)
        objDS = objBUS.SelectAll()
        ' Neu ton tai du lieu ung voi chuc vu table trong DB
        dtgvDataList.AutoGenerateColumns = False
        dtgvDataList.DataSource = objDS.Tables(0)
        dtgvDataList.ClearSelection()
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        ' Neu user chua select in grid view -> thuc hien thao tac insert
        If txtMaDIADIEM.Text = "" Then
            InsertDIADIEM()
        Else ' Thuc hien thao tac update
            UpdateDIADIEM()
        End If

    End Sub

    Public Sub InsertDIADIEM()
        Dim objBUS As DIADIEMBUS = New DIADIEMBUS(GlobalVars.g_strConnectionString)
        Dim objDTO As DIADIEMDTO = New DIADIEMDTO
        objDTO.TenDD = txtTenDIADIEM.Text
        objDTO.DiaChi = txtDiaChi.Text
        objDTO.QuocGia = txtQuocGia.Text

        If objBUS.Insert(objDTO) Then
            LoadData()
            EraseTextboxes()
            MessageBox.Show("An item has been Added")
        Else
            MessageBox.Show("Add failed")
        End If
    End Sub

    Public Sub UpdateDIADIEM()
        Dim objBUS As DIADIEMBUS = New DIADIEMBUS(GlobalVars.g_strConnectionString)
        Dim objDto As DIADIEMDTO = New DIADIEMDTO
        objDto.ID = Integer.Parse(txtMaDIADIEM.Text)
        objDto.TenDD = txtTenDIADIEM.Text
        objDto.DiaChi = txtDiaChi.Text
        objDto.QuocGia = txtQuocGia.Text

        If objBUS.Update(objDto) Then
            LoadData()
            MessageBox.Show("A new item has been updated")
        Else
            MessageBox.Show("Update failed!")
        End If
    End Sub

    ' Lay thong tin cua chuc vu khi nguoi dung select in grid view
    Private Sub dtgvDIADIEM_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgvDataList.CellClick
        Dim selectedRow As Integer = dtgvDataList.CurrentRow.Index
        If selectedRow <> -1 Then
            txtMaDIADIEM.Text = dtgvDataList.Item(0, selectedRow).Value
            txtTenDIADIEM.Text = dtgvDataList.Item(1, selectedRow).Value
            txtDiaChi.Text = dtgvDataList.Item(2, selectedRow).Value
            txtQuocGia.Text = dtgvDataList.Item(3, selectedRow).Value
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim objBUS As DIADIEMBUS = New DIADIEMBUS(GlobalVars.g_strConnectionString)
        ' Chi thuc hien xoa, khi user select in grid view
        If txtMaDIADIEM.Text <> "" Then
            If objBUS.Delete(Integer.Parse(txtMaDIADIEM.Text)) Then
                LoadData()
                EraseTextboxes()
                MessageBox.Show("A new item has been deleted")
            Else
                MessageBox.Show("Delete failed!")
            End If
        End If

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnNhapMoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapMoi.Click
        EraseTextboxes()
    End Sub

    Public Sub EraseTextboxes()
        txtMaDIADIEM.Text = ""
        txtTenDIADIEM.Text = ""
        txtDiaChi.Text = ""
        txtQuocGia.Text = ""
        dtgvDataList.ClearSelection()
    End Sub

    Private Sub btnChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChon.Click
        Close()
        RaiseEvent Handler(Me, New MyEvent(txtMaDIADIEM.Text & "#" & txtTenDIADIEM.Text))
    End Sub
End Class